package client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;

/**
 * @author srh
 * @date 2020/10/18
 **/
public class NettyClient {

    public static void main(String[] args) throws InterruptedException {
        // 创建客户端启动辅助类
        Bootstrap bootstrap = new Bootstrap();
        // 创建线程池实例
        NioEventLoopGroup group = new NioEventLoopGroup();
        // 将线程池添加到组中
        bootstrap.group(group)
                // 指定通道类型
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<Channel>() {
                    @Override
                    protected void initChannel(Channel channel) throws Exception {
                        channel.pipeline().addLast(new StringEncoder());
                    }
                });
        // 连接通道
        Channel channel = bootstrap.connect("127.0.0.1", 8000).channel();

        for (int i = 0; i < 6; i++) {
            channel.writeAndFlush("Timestamp= " + System.currentTimeMillis());
            Thread.sleep(3000);
        }
    }

}
